//
//  JEmoticons.swift
//  01-表情键盘
//
//  Created by Jason on 29/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit

class JEmoticons: NSObject {
    /***  分组名称 */
    var emoticon_group_name: String?
    /***   分组路径 */
    var emoticon_group_path: String?
    /***  表情文本 */
    var chs: String?
    /***  图片名称 */
    var png: String?
    /***  emoji 代码 */
    var code: String?
    /// 是否删除按钮
    var removeFlag = false
    // emoji字符串
    var emojiStr : String?
    // 图片路径  = emoticon_group_path + png
    var imgPath : String? {
        // 如果有图片
        if png != nil {
            return emoticon_group_path!.stringByAppendingPathComponent(png!)
        }
        return nil
    }
    init(groupName:String,groupPath:String,dict:[String:String]?) {
        super.init()
        emoticon_group_name = groupName
        emoticon_group_path = groupPath
        
        chs = dict?["chs"]
        png = dict?["png"]
        code = dict?["code"]
        // 设置emojiStr
        if code != nil {
            // scanner专门用来扫描字符串的
            let scanner = NSScanner(string: code!)
            // 扫描其中的字符串
            var result :UInt32 = 0
            scanner.scanHexInt(&result)
            
            emojiStr = "\(Character(UnicodeScalar(result)))"
        }
    }
    /**
    返回所有的Emoticon
    
    :returns: JEmoticons对象数组
    */
    class func emoticonList() ->[JEmoticons] {
        var list = [JEmoticons]()
        // 从文件加载emticons.plist文件
        let path = NSBundle.mainBundle().pathForResource("emoticons", ofType: "plist", inDirectory: "Emoticons.bundle")
        let arrsM = (NSArray(contentsOfFile: path!) as! [[String:String]]).sorted { (dict1, dict2) -> Bool in
            return dict1["emoticon_group_type"] < dict2["emoticon_group_type"]
        }
        
        // 遍历表情数组
        for dict in arrsM {
            let groupPath = NSBundle.mainBundle().bundlePath.stringByAppendingPathComponent("Emoticons.bundle").stringByAppendingPathComponent(dict["emoticon_group_path"]!)
            list += groupEmoticonList(dict["emoticon_group_name"]!, groupPath: groupPath)
        }
        
        return list
    }
    
    private class func groupEmoticonList(groupName:String,groupPath:String) -> [JEmoticons] {
        var specList = [JEmoticons]()
        // 1.加载info.plist
        let infoPath = groupPath.stringByAppendingPathComponent("info.plist")
        let dict = NSDictionary(contentsOfFile: infoPath)!
        // 取出表情符号数组
        let array = dict["emoticon_group_emoticons"] as! [[String:String]]
        
        // 2.遍历表情数组
        /**
           - 每页需要21个表情
           - 每遍历20个表情需要插入一个删除表情
           - 如果不足20个,需要插入空表情,
           - 目标:每个分组的表情是21的整数倍
         */
        var count = 0
        for dict in array {
            specList.append(JEmoticons(groupName: groupName, groupPath: groupPath, dict: dict))
            count++
            // 第21个需要生成一个删除表情
            if count == 20 {
                let e = JEmoticons(groupName: groupName, groupPath: groupPath, dict: nil)
                // 设置为删除按钮
                e.removeFlag = true
                // 添加到数组中
                specList.append(e)
                // 重置count
                count = 0
            }
        }
        // 如果当前数组不能被21整除,需要补足表情
        var emptyCount = specList.count % 21
        if emptyCount != 0 {
            println("需要补足表情")
            // 创建空表情
            for i in emptyCount..<21 {
                let e = JEmoticons(groupName: groupName, groupPath: groupPath, dict: nil)
                specList.append(e)
            }
            // 最后一个设置为删除按钮
            specList.last?.removeFlag = true
        }
        return specList
    }
}


